<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 封装一个栈的方法
        function Stack() {
            this.items = [];
            // 添加push方法---入栈
            Stack.prototype.push = function (element) {
                this.items.push(element)
            }
            // 添加pop方法 ----出栈
            Stack.prototype.pop = function () {
                return this.items.pop()
            }
            // isEmpty()判断栈中元素是否为空
            Stack.prototype.isEmpty = function () {
                return this.items.length === 0
            }
        }


        // 封装十进制转二进制的函数---直接调用上面的方法就行
        function dec2bin(decNumer) {
            // 定义变量
            var stack = new Stack()
            var remainder;

            // 循环除法
            while (decNumer > 0) {
                remainder = decNumer % 2
                decNumer = Math.floor(decNumer / 2)
                stack.push(remainder)
            }

            // 将数据取出
            var binayriStrng = ""
            while (!stack.isEmpty()) {
                binayriStrng += stack.pop()
            }
            return binayriStrng
        }

        // 测试函数
        console.log(dec2bin(10));
        console.log(dec2bin(100));
        console.log(dec2bin(1000));



    </script>
</body>

</html>